<template>
  <div class="bank-cards">
    <ul class="card-list">
      <li v-for="item in cardList" :key="item.number">
        <p>{{item.accountType | accountTypeFilter}}</p>
        <p>{{item.openAccountBank}}</p>
        <p>{{item.cardNumber}}</p>
        <span class="pointer text-gray" @click="openCheckModal(2,item)">解除绑定</span>
        <span class="pointer text-gray" @click="openCheckModal(1,item)">修改</span>
      </li>
      <li class="add-card pointer" @click="openCheckModal(1)">
        <div class="center">
          <a-icon type="plus" />
          <p>添加银行卡</p>
        </div>
      </li>
    </ul>
    <check-phone :visible="checkVisible" @cancel="closeCheckModal" />
    <card-info :visible="infoVisible" :record="currentCard" @cancel="closeInfoModal" />
  </div>
</template>
<script>
import CardInfo from './components/CardInfo'
import CheckPhone from './components/CheckPhone'
export default {
  name: 'bank-cards',
  components: {
    CheckPhone,
    CardInfo
  },
  filters: {
    accountTypeFilter (type) {
      return ['', '对私', '对公'][type]
    }
  },
  data () {
    return {
      checkFlag: null, // 1 添加银行卡， 2 解绑银行卡， 3 修改银行卡
      currentCard: null,
      checkVisible: false, // 解绑银行卡短信验证显示
      infoVisible: false, // 添加银行卡
      cardList: [],
    }
  },
  mounted () {
    this.getCardList()
  },
  methods: {
    getCardList () {
      this.$get('/bank/card/list').then(res => {
        if (res && res.returnCode === 1000) {
          this.cardList = res.dataInfo
        }
      })
    },
    // 打开校验弹窗
    openCheckModal (checkFlag, card = null) {
      console.log('acrd--', card)
      this.checkFlag = checkFlag
      this.currentCard = card
      this.checkVisible = true 
    },
    // flag 校验成功与否
    closeCheckModal (flag) {
      const checkFlag = this.checkFlag
      this.checkVisible = false
      this.checkFlag = null
      if (!flag) return  
      switch (checkFlag) {
        case 1:
          this.addCard()
          break;
        case 2:
          this.unbindCard()
          break;
        case 3:
          
          break;
      
      }
    },
    // 解绑银行卡
    unbindCard () {
      if (!(this.currentCard && this.currentCard.id)) return
      this.$post('/bank/card/delete', [this.currentCard.id]).then(res => {
        if (res && res.returnCode === 1000) {
          this.$message.success('解绑成功！')
          this.getCardList()
        }
      })
    },
    // 添加银行卡
    addCard () {
      this.infoVisible = true
    },
    // 关闭卡片信息弹窗
    closeInfoModal (flag) {
      this.infoVisible = false
      this.currentCard = null
      if (flag) {
        this.getCardList()
      }
    }
  }
}
</script>
<style lang="less" scoped>
.bank-cards {
  .card-list {
    display: flex;
    flex-wrap: wrap;
    li {
      width: 250px;
      height: 150px;
      border-radius: 6px;
      background-color: #f0f5f8;
      padding: 10px;
      margin-right: 20px;
      margin-bottom: 20px;
      &.add-card {
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 16px;
        color: #333;
      }
    }
  }
}
</style>